<template>
  <div style="width: 200px">
    <button @click="toggleBox">{{ isVisible ? '隐藏' : '显示' }} 盒子</button>
    <transition name="fade">
      <div v-if="isVisible" class="box"></div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'fadeView',
  data() {
    return {
      isVisible: false, // 控制盒子是否可见
    }
  },
  methods: {
    toggleBox() {
      this.isVisible = !this.isVisible // 切换可见状态
    },
  },
}
</script>

<style lang="scss" scoped>
.box {
  width: 100px;
  height: 100px;
  border: 1px solid;
  margin: 10px auto;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 1s; /* 渐变效果 */
}

.fade-enter, .fade-leave-to /* .fade-leave-active 在 Vue 2.1.8+ */ {
  opacity: 0; /* 初始和结束时的透明度 */
}
</style>
